﻿using JinJia.Core.MQProvider.Consumer;
using Jinjia.Approve.IService;
using Microsoft.Extensions.DependencyInjection;
using RabbitMQ.Client.Events;
using System;

namespace Jinjia.Approve.Service.Consumer
{
    public class HelloWorldConsumer : IRabbitMQConsumerService
    {
        private readonly IServiceProvider _serviceProvider;

        public HelloWorldConsumer(IServiceProvider serviceProvider)
        {
            this._serviceProvider = serviceProvider;
        }

        public int ConsumerNum => 1;

        public int HorizontalSplit => 0;

        public bool VerticalSplit => true;

        public string ClientName => "请输入客户端名";

        public string QueueName => "请输入队列名";

        /// <summary>
        /// 消息处理逻辑
        /// </summary>
        /// <param name="o"></param>
        /// <param name="e"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public string Consumer(object o, BasicDeliverEventArgs e, string message)
        {
            #region 数据转换

            //var data = Newtonsoft.Json.JsonConvert.DeserializeObject<VM>(message);
            //if (data == null) return "";

            #endregion

            #region 获取服务

            var scopeProvider = _serviceProvider.CreateScope().ServiceProvider;
            var helloWorldService = scopeProvider.GetService<IHelloWorldService>();

            #endregion

            //helloWorldService.InsertMysqlHelloWorld(null);

            return "";
        }

        /// <summary>
        /// 回调-消费成功
        /// </summary>
        /// <param name="message"></param>
        public void CustomerSuccess(string message)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 回调-消费失败
        /// </summary>
        /// <param name="e"></param>
        /// <param name="message"></param>
        public void CustomerFail(Exception e, string message)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 消费者启动失败
        /// </summary>
        /// <param name="o"></param>
        /// <param name="e"></param>
        public void CallCackException(object o, CallbackExceptionEventArgs e)
        {
        }
    }
}